﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CRM.Database.Model
{
    public class EquipmentTypesCollection : AbstractCollection
    {
        /// <summary>
        /// Список типов оборудования
        /// </summary>
        public List<EquipmentTypes> EquipmentTypesList
        {
            get { return Entities.EquipmentTypes.ToList(); }
        }

        /// <summary>
        /// список активных объектов
        /// </summary>
        public List<EquipmentTypes> EquipmentTypesActiveList
        {
            get
            {
                var result = new List<EquipmentTypes>();
                if (EquipmentTypesList.Count < 1) return result;
                result.AddRange(EquipmentTypesList.Where(item => item.IsActive));
                return result;
            }
        }

        #region Implementation of IGetItem

        /// <summary>
        /// Получение типа текущего элемента
        /// </summary>
        /// <returns></returns>
        protected override Type GetItemType()
        {
            return typeof (EquipmentTypes);
        }

        /// <summary>
        /// Получение статического списка для хранения объектов
        /// </summary>
        /// <returns></returns>
        public override object GetStaticCollection()
        {
            return null;
        }

        /// <summary>
        /// Получение элемента из коллекции по идентификатору
        /// </summary>
        /// <param name="itemId">Идентификатор</param>
        /// <returns>Элемент</returns>
        public override object GetItemById(int itemId)
        {
            return Entities.EquipmentTypes.Find(itemId);
        }

        #endregion

        #region Implementation of IAddDeleteUpdate

        /// <summary>
        /// Обновление элемента в коллекции
        /// </summary>
        /// <param name="item">Новый элемент</param>
        /// <param name="userLogin">Логин пользователя, который делает изменения</param>
        /// <returns>Результат операции</returns>
        public bool Update(object item, string userLogin)
        {
            var newEquipmentType = (EquipmentTypes) item;
            if (newEquipmentType == null)
                return false;


            var EquipmentType4Update = GetItemByIdFromDatabase(newEquipmentType.IdEquipmentType) as EquipmentTypes;

            if (EquipmentType4Update == null)
                return true;

            if (!string.IsNullOrEmpty(newEquipmentType.EquipmentTypeRu))
                EquipmentType4Update.EquipmentTypeRu = newEquipmentType.EquipmentTypeRu;

            if (!string.IsNullOrEmpty(newEquipmentType.EquipmentTypeEn))
                EquipmentType4Update.EquipmentTypeEn = newEquipmentType.EquipmentTypeEn;

            EquipmentType4Update.IsActive = newEquipmentType.IsActive;


            return base.Update(EquipmentType4Update, userLogin);
        }

        /// <summary>
        /// Обнуление статической коллекции
        /// </summary>
        protected override void SetStaticListToNULL()
        {
        }

        #endregion

        #region Implementation of IGetIdByName

        /// <summary>
        /// Получение идентификатора по имени
        /// </summary>
        /// <param name="name">Имя</param>
        /// <returns>Идентификатор</returns>
        public int GetIdByName(string name)
        {
            EquipmentTypes obj =
                EquipmentTypesList.FirstOrDefault(c => c.EquipmentTypeRu == name || c.EquipmentTypeEn == name);
            if (obj != null)
                return obj.IdEquipmentType;
            return 0;
        }

        /// <summary>
        /// Получение списка элементов
        /// </summary>
        /// <returns>Список элементов</returns>
        public override List<object> GetList()
        {
            return new List<object>(EquipmentTypesList);
        }

        #endregion
    }
}